// pages/u_rzDriver/u_rzDriver.js
import { multiArray, objectMultiArray} from '../../utils/pickerLinkage.js'
const app = getApp()
var flag = 1;var driver_register;var save_driver_img;var get_conf_img;

function uploadimg(data) {
  console.log(data);
  var that = this,
    i = data.i ? data.i : 0,
    success = data.success ? data.success : 0,
    fail = data.fail ? data.fail : 0;
  wx.uploadFile({
    url: data.url,
    filePath: data.path[i],
    name: 'file',
    formData: {
      id: data.id,
      type: data.type
    },
    success: function (res) {
      success++;
      console.log(res)
      console.log(i);
      console.log(data.id);
    },
    fail: function (res) {
      fail++;
      console.log('fail:' + i + "fail:" + fail);
    },
    complete: function (res) {
      console.log(i);
      i++;
      if (i == data.path.length) {   //当图片传完时，停止调用   
        console.log('执行完毕');
        console.log('成功：' + success + " 失败：" + fail);
      } else {
        console.log(i);
        data.i = i;
        data.success = success;
        data.fail = fail;
        uploadimg(data);
      }

    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['男', '女'],
    index:0,
    region: ['请选择省市区', '', ''],
    y_area:"请选择",
    tempFilePaths:[],
    multiIndex: [0, 0],
    multiArray: multiArray,     
    objectMultiArray: objectMultiArray,
    flist:['北京'],
    fix:false,
    name:'',
    mobile:'',
    car:'',
    jsz:'',
    addr:'',
    content:'',
    tempFilePaths:[],
    thumb1:[],
    thumb2:[],
    thumb3:[],
    select:false,
    jsz_img:'',
    xsz_img:'',
    sfz_img:'',
  },
  xztap:function(e){
    this.setData({
      select:!this.data.select
    })
  },
  hidetap:function(e){
    this.setData({
      fix:false
    })
  },
  showtap:function(e){
    this.setData({
      fix:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    flag = 1;
    driver_register = app.data.ucenter_url + 'driver_register';
    save_driver_img = app.data.ucenter_url + 'save_driver_img';
    get_conf_img = app.data.ucenter_url + 'get_conf_img';
    wx.request({
      url: get_conf_img,
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data);
        var list = res.data.list;
        var img_url = app.data.img_url;
        for(var i=0;i<list.length;i++){
          if(list[i].name == 'jsz'){
            var jsz_img = img_url+list[i].value;
            that.setData({
              jsz_img:jsz_img
            })
          }
          if(list[i].name == 'xsz'){
            var xsz_img = img_url+list[i].value;
            that.setData({
              xsz_img:xsz_img
            })
          }
          if(list[i].name == 'sfz'){
            var sfz_img = img_url+list[i].value;
            that.setData({
              sfz_img:sfz_img
            })
          }
        }
      }
    })

    
  },
  xytap:function(e){
    wx.navigateTo({
      url: '/pages/i_xy/i_xy?name=sjrzxy',
    })
  },
  chooseimage: function (e) {
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;
    var img = tempFilePaths;
    wx.chooseImage({
      count: 6, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        for (var i = 0; i < imgsrc.length; i++) {
          img.push(imgsrc[i])
        }
        console.log(img);
        that.setData({
          tempFilePaths: img
        })
      }
    })
  },
  chooseimage1: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          thumb1: res.tempFilePaths
        })
      }
    })
  },
  chooseimage2: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          thumb2: res.tempFilePaths
        })
      }
    })
  },
  chooseimage3: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          thumb3: res.tempFilePaths
        })
      }
    })
  },
  nametap:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  mobiletap:function(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  cartap:function(e){
    this.setData({
      car:e.detail.value
    })
  },
  jsztap:function(e){
    this.setData({
      jsz:e.detail.value
    })
  },
  addrtap:function(e){
    this.setData({
      addr:e.detail.value
    })
  },
  contenttap:function(e){
    this.setData({
      content:e.detail.value
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindMultiPickerChange: function(e) {
    var that = this;
    var multiArray = that.data.multiArray;
    var multiIndex = that.data.multiIndex;
    var flist = that.data.flist;
    var area = multiArray[1][multiIndex[1]];
    flist.push(area);
    that.setData({ 
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1],
      flist:flist
    })
  },   
  bindMultiPickerColumnChange: function(e) {
    var that = this;
    var list = [];
    switch (e.detail.column) {
      case 0: list = []
      for (var i = 0; i < that.data.objectMultiArray.length; i++) {
        if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].regid) {
          list.push(that.data.objectMultiArray[i].regname)
        }
      }
      that.setData({
        "multiArray[1]": list,
        "multiIndex[0]": e.detail.value, "multiIndex[1]": 0
      })
    }
  },
  deltap:function(e){
    var that = this;
    var flist = that.data.flist;
    var index = e.currentTarget.dataset.index;
    flist.splice(index,1);
    that.setData({
      flist:flist
    })
  },
  fsubmit:function(e){
    var that = this;
    var flist = that.data.flist;
    var y_area = '';
    for(var i=0;i<flist.length;i++){
      y_area+=flist[i]+',';
    }
    y_area = y_area.substring(0,y_area.length-1);
    that.setData({
      y_area:y_area,
      fix:false
    })
  },
  submit:function(e){
    var that = this;
    if(flag == 1){
      flag = 0;
      var name = that.data.name;
      if(name == ''){
        wx.showModal({
          title: '提示',
          content: '请输入姓名',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var array = that.data.array;
      var index = that.data.index;
      var sex = array[index];
      var mobile = that.data.mobile;
      if(mobile == ''){
        wx.showModal({
          title: '提示',
          content: '请输入手机号码',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var car_num = that.data.car;
      if(car_num == ''){
        wx.showModal({
          title: '提示',
          content: '请输入车牌号码',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var drive_num = that.data.jsz;
      if(car_num == ''){
        wx.showModal({
          title: '提示',
          content: '请输入驾驶证号',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var region = that.data.region;
      if(region[0] == '请选择省市区'){
        wx.showModal({
          title: '提示',
          content: '请选择省市区',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var city = region[0]+'/'+region[1]+'/'+region[2];
      var address = that.data.addr;
      if(address == ''){
        wx.showModal({
          title: '提示',
          content: '请输入您的详细地址',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var area = that.data.y_area;
      if(area == '请选择'){
        wx.showModal({
          title: '提示',
          content: '请选择优势地区',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var thumb1 = that.data.thumb1;
      if(thumb1.length == 0){
        wx.showModal({
          title: '提示',
          content: '请上传驾驶证照片',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var thumb2 = that.data.thumb2;
      if(thumb2.length == 0){
        wx.showModal({
          title: '提示',
          content: '请上传行驶证照片',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var thumb3 = that.data.thumb3;
      if(thumb3.length == 0){
        wx.showModal({
          title: '提示',
          content: '请上传身份证正面',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var content = that.data.content;
      if(content == ''){
        wx.showModal({
          title: '提示',
          content: '请输入服务介绍',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var tempFilePaths = that.data.tempFilePaths;
      if(tempFilePaths.length == 0){
        wx.showModal({
          title: '提示',
          content: '请上传图片展示',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var select = that.data.select;
      if(select == false){
        wx.showModal({
          title: '提示',
          content: '请阅读并同意《司机入住协议》',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var aid = wx.getStorageSync('uid');
      wx.showLoading();
      wx.request({
        url: driver_register,
        data: {
          uid:aid,
          name:name,
          mobile:mobile,
          sex:sex,
          car_num:car_num,
          drive_num:drive_num,
          city:city,
          address:address,
          area:area,
          content:content
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data);
          if(res.data.state == 1){
            var id = res.data.id;
            wx.uploadFile({
              url: save_driver_img,
              filePath: thumb1[0],
              name: 'file',
              formData: {
                id:id,
                type:1
              },
              success(res) {
                console.log('驾驶证' + res);
              }
            })
            wx.uploadFile({
              url: save_driver_img,
              filePath: thumb2[0],
              name: 'file',
              formData: {
                id:id,
                type:2
              },
              success(res) {
                console.log('行驶证' + res);
              }
            })
            wx.uploadFile({
              url: save_driver_img,
              filePath: thumb3[0],
              name: 'file',
              formData: {
                id:id,
                type:3
              },
              success(res) {
                console.log('身份证' + res);
              }
            })
            uploadimg({
              url: save_driver_img,//这里是你图片上传的接口
              path: tempFilePaths,//这里是选取的图片的地址数组
              id: id,
              type:4
            });
            flag = 1;
            setTimeout(function(e){
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '信息提交成功，请等待后台审核！',
                showCancel: false,
                success: function (e) {
                  wx.switchTab({
                    url: '/pages/ucenter/ucenter',
                  })
                }
              })
            },500)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})